package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.bo.IotElevatorTiringBo;
import org.dromara.iot.domain.vo.IotElevatorTiringVo;
import org.dromara.iot.service.IIotElevatorTiringService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电梯困人管理
 *
 * @author lsl
 * @date 2024-06-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/elevatorTiring")
public class IotElevatorTiringController extends BaseController {

    private final IIotElevatorTiringService iotElevatorTiringService;

    /**
     * 查询电梯困人管理列表
     */
    @SaCheckPermission("iot:elevatorTiring:list")
    @GetMapping("/list")
    public TableDataInfo<IotElevatorTiringVo> list(IotElevatorTiringBo bo, PageQuery pageQuery) {
        return iotElevatorTiringService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯困人管理列表
     */
    @SaCheckPermission("iot:elevatorTiring:export")
    @Log(title = "电梯困人管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotElevatorTiringBo bo, HttpServletResponse response) {
        List<IotElevatorTiringVo> list = iotElevatorTiringService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯困人管理", IotElevatorTiringVo.class, response);
    }

    /**
     * 获取电梯困人管理详细信息
     *
     * @param tiringId 主键
     */
    @SaCheckPermission("iot:elevatorTiring:query")
    @GetMapping("/{tiringId}")
    public R<IotElevatorTiringVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long tiringId) {
        return R.ok(iotElevatorTiringService.queryById(tiringId));
    }

    /**
     * 修改电梯困人管理
     */
    @SaCheckPermission("iot:elevatorTiring:edit")
    @Log(title = "电梯困人管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotElevatorTiringBo bo) {
        return toAjax(iotElevatorTiringService.updateByBo(bo));
    }

    /**
     * 删除电梯困人管理
     *
     * @param tiringIds 主键串
     */
    @SaCheckPermission("iot:elevatorTiring:remove")
    @Log(title = "电梯困人管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tiringIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] tiringIds) {
        return toAjax(iotElevatorTiringService.deleteWithValidByIds(List.of(tiringIds), true));
    }

    /**
     * 上报困人信息
     * @TODO 告警
     */
    @SaCheckPermission("iot:elevatorTiring:add")
    @Log(title = "电梯困人管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> reportTiring(@RequestBody IotElevatorTiringBo bo) {
        return toAjax(iotElevatorTiringService.reportTiring(bo));
    }

    /**
     * 开始救援
     */
    @Log(title = "电梯困人管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/starRescue")
    public R<Void> starRescue(@RequestBody IotElevatorTiringBo bo) {
        return toAjax(iotElevatorTiringService.starRescue(bo));
    }

    /**
     * 救援签到
     */
    @Log(title = "电梯困人管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/rescueSign")
    public R<Void> rescueSign(@RequestBody IotElevatorTiringBo bo) {
        return toAjax(iotElevatorTiringService.rescueSign(bo));
    }

    /**
     * 救援完成
     */
    @Log(title = "电梯困人管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/rescueComplete")
    public R<Void> rescueComplete(@RequestBody IotElevatorTiringBo bo) {
        return toAjax(iotElevatorTiringService.rescueComplete(bo));
    }

    /**
     * 确认救援完成
     */
    @Log(title = "电梯困人管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/confirmComplete")
    public R<Void> confirmComplete(@RequestBody IotElevatorTiringBo bo) {
        return toAjax(iotElevatorTiringService.confirmComplete(bo));
    }
}
